//
//  BaseViewController.m
//  Example
//
//  Created by Andney Jack on 2020/6/2.
//  Copyright © 2020 annidy. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self m_loadUI];
}

- (void)m_loadUI {
    UIView *contentView = [UIView new];
    contentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4f];
    [self.view addSubview:contentView];

    UIView *preView = nil;
    NSInteger rowCount = 5;
    CGFloat itemHeight = 40; // Item的高度
    CGFloat spacingX = 10;
    CGFloat spacingY = 10;
    CGFloat padding = 20;
    NSInteger index = 0;
    for (NSString *title in self.actionTitles) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor purpleColor];
        btn.layer.cornerRadius = 4;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(actionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:btn];

        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (preView == nil) {
                make.left.equalTo(contentView).offset(padding);
                make.height.mas_equalTo(itemHeight);
                make.top.equalTo(contentView).offset(25);
                make.width.equalTo(contentView).offset((-2 * padding - (rowCount - 1) * spacingX) * 1.0f / rowCount).dividedBy(rowCount);
            } else {
                // 换行时的判断
                if (index % rowCount == 0) {
                    make.left.equalTo(contentView).offset(padding);
                    make.width.height.equalTo(preView);
                    make.top.equalTo(preView.mas_bottom).offset(spacingY);
                } else {
                    make.left.equalTo(preView.mas_right).offset(spacingX);
                    make.width.height.and.top.equalTo(preView);
                }
            }
        }];

        index++;
        preView = btn;
    }

    [preView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(contentView).offset(-20);
    }];

    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_leftMargin).offset(-20);
        make.right.equalTo(self.view.mas_rightMargin).offset(20);
        make.bottom.equalTo(self.view.mas_bottomMargin);
    }];
}

- (void)actionButtonClick:(UIButton *)sender {
    NSString *action = sender.currentTitle;
    [self handleAction:action];
}

- (void)handleAction:(NSString *)action {
}

- (void)closePage:(BOOL)animated
{
    if (self.navigationController) {
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            if (self.navigationController.presentingViewController) {
                [self.navigationController dismissViewControllerAnimated:animated completion:nil];
            }
        }
    } else {
        if (self.presentingViewController) {
            [self dismissViewControllerAnimated:animated completion:nil];
        }
    }
}

- (NSMutableArray<NSString *> *)actionTitles
{
    if (!_actionTitles) {
        _actionTitles = [NSMutableArray new];
    }
    return _actionTitles;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

@end
